package com.echoplots.enjoy.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

import com.echoplots.enjoy.entity.User;
@Controller
public class IndexController {

	@GetMapping({ "", "/", "index" })
	public String index(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/login";
		}
		request.setAttribute("user", user);

		return "index";
	}

	@GetMapping("/login")
	public String login() {
		return "login";
	}

	@PostMapping("/login")
	public String save(@ModelAttribute User modelAttribute, BindingResult result, HttpServletRequest request) {
		if (result.hasErrors()) {
			return "binding error";
		}
		request.getSession().setAttribute("user", modelAttribute);
		return "redirect:/";
	}

}
